/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* Generated By:JJTree: Do not edit this line. AstMethodSuffix.java */

package org.jboss.el.parser;

import java.lang.reflect.Method;

import javax.el.ELException;
import javax.el.MethodInfo;
import javax.el.PropertyNotFoundException;
import javax.el.PropertyNotWritableException;

import org.jboss.el.lang.EvaluationContext;
import org.jboss.el.util.MessageFactory;
import org.jboss.el.util.ReflectionUtil;



@SuppressWarnings("rawtypes")
public class AstMethodSuffix extends ValueSuffixNode {

	public AstMethodSuffix(int id) {
		super(id);
	}

	private Object[] getParameters(EvaluationContext ctx) throws ELException {
		if (this.children == null)
			return null;
		Object[] obj = new Object[this.children.length];
		for (int i = 0; i < obj.length; i++) {
			obj[i] = this.children[i].getValue(ctx);
		}
		return obj;
	}

	public MethodInfo getMethodInfo(Object base, EvaluationContext ctx,
			Class[] paramTypes) throws ELException {
		return ReflectionUtil.getMethodInfo(base, this.image, this.getParameters(ctx));
	}

	public Object getTarget(Object base, EvaluationContext ctx)
			throws ELException {
		Object r = ReflectionUtil.invokeMethod(base, this.image, this
				.getParameters(ctx));
		if (r == null) {
			if (r == null) {
				throw new PropertyNotFoundException(
						"Target Unreachable, Method '" + this.image
								+ "' returned null for type " + base.getClass().getName());
			}
		}
		return r;
	}

	public Class getType(Object base, EvaluationContext ctx) throws ELException {
		Method m = ReflectionUtil.findMethod(base, this.image, this
				.getParameters(ctx));
		return m.getReturnType();
	}

	public Object getValue(Object base, EvaluationContext ctx)
			throws ELException {
		return ReflectionUtil.invokeMethod(base, this.image, this
				.getParameters(ctx));
	}

	public Object invoke(Object base, EvaluationContext ctx,
			Class[] paramTypes, Object[] paramValues) throws ELException {
		return this.getValue(base, ctx);
	}

	public boolean isReadOnly(Object base, EvaluationContext ctx)
			throws ELException {
		return true;
	}

	public void setValue(Object base, EvaluationContext ctx, Object value)
			throws ELException {
		throw new PropertyNotWritableException(MessageFactory
				.get("error.syntax.set"));
	}

}
